package com.hyd.issues.auth;

import com.hyd.issues.BaseAction;
import com.hyd.issues.json.JsonResult;
import com.hyd.issues.user.User;

/**
 * (description)
 *
 * @author yiding.he
 */
public class LoginAction extends BaseAction {

    public JsonResult login() {
        String username = p("username");
        String password = p("password");

        if (User.checkLogin(username, password)) {
            saveUserToSession(username);
            return JsonResult.SUCCESS;
        } else {
            return JsonResult.fail("用户名或密码不正确。");
        }
    }

    public JsonResult signup() {
        String username = p("username");
        String password = p("password");

        if (User.existsByName(username)) {
            return JsonResult.fail("用户名已存在。");
        }

        User.saveUser(username, password);
        return JsonResult.SUCCESS;
    }

    private void saveUserToSession(String username) {
        session.put(User.SESSION_KEY, User.getUser(username));
    }
}
